using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Unity.Per.Common.UIFrameWork
{
    public abstract class BaseWindow :BaseModuleMono{

        [SerializeField]
        private BindWindow m_bindWindow;
        public BindWindow bindWindow {
            get {
                if ( m_bindWindow == null ) {
                    UpdateBindWindow(GetComponentInParent<BindWindow>());
                }
                return m_bindWindow;
            }
            set {
                UpdateBindWindow(value);
            }
        }


        [SerializeField]
        private List<BasePanelSlot> m_panelBlockList;
        public List<BasePanelSlot> panelBlockList {
            get {
                if ( m_panelBlockList == null ) {
                    m_panelBlockList = new List<BasePanelSlot>();
                }
                return m_panelBlockList;
            }
            set {
                m_panelBlockList = value;
            }
        }

        void UpdateBindWindow(BindWindow bindWindow) {
            if ( bindWindow == m_bindWindow )
                return;

            if ( m_bindWindow ) {
                m_bindWindow.RemoveModule(this);
            }

            m_bindWindow = bindWindow;

            if ( m_bindWindow ) {
                m_bindWindow.AddModule(this);
            }
        }


        protected override void OnModuleStart() {
            base.OnModuleStart();
            gameObject.SetActive(true);

            if ( panelBlockList.Count == 0 )
                return;

            foreach ( var panelBlock in panelBlockList ) {
                panelBlock.ModuleStart();
            }

        }

        protected override void OnModuleStop() {
            base.OnModuleStop();
            gameObject.SetActive(false);

            if ( panelBlockList.Count == 0 )
                return;

            foreach ( var panelBlock in panelBlockList ) {
                panelBlock.ModuleStop();
            }

        }

        public void AddPanelSlot(BasePanelSlot panelBlock) {
            if ( panelBlock ==null) {
                return;
            }
            if ( panelBlockList.Contains(panelBlock) == false ) {
                panelBlockList.Add(panelBlock);
            }
        }

        public void RemovePanelSlot(BasePanelSlot panelBlock) {
            if ( panelBlock == null ) {
                return;
            }
            if ( panelBlockList.Contains(panelBlock)) {
                panelBlockList.Remove(panelBlock);
            }
        }

    }

}
